var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var WxParse = require('../../wxParse/wxParse.js');
var base64 = require('../../utils/Base64.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    util: util,
    tc: 0,

    bottom_nav_data: [],
    isIphone_x: false,
    nav_bgColor: '#ffffff',

    version: util.version,
    token: '',
    uid: 0,
    userinfo: {},
  
    loginUserInfo: '',
    uid: 0,  //当前登录用户Id
    token: '', //post请求时，当前登录用户token

    liveListInfo: '',


    img_wh: 150,

    currcata: 0,
    isIphone_x: false,

    swipe_img_maxH: 160,

    swipelist_minH: 300,


    indicatorDots: true,
    autoplay: true,
    interval: 4000,
    duration: 600,
    startTime:0, 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var tc = app.get_tc();
    that.setData({
      tc: tc,
    });
    var inviteuid = options.inviteuid;
    //调用应用实例的方法获取全局数据
    app.LoginUserInfo(function (res) {
      //更新数据
      that.setData({
        loginUserInfo: res,
        uid: res.uid,
        token: res.token
      })
    }, inviteuid);

    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          img_wh: (res.windowWidth - 26) / 2,
          swipe_img_maxH: res.windowWidth / 2,
          swipelist_minH: res.windowHeight - (res.windowWidth/2 + 39 + 50)
        })
      },
    });

    //
    that.getzhibopic();
    //获取直播列表
    that.getLiveList();

    wx.getSystemInfo({
      success: function (res) {

        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
        });

      }
    })
    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color,
      })
      that.resetNavColor();
    });
    //初始化底部导航
    var b_nav_data = app.getBottomNavData('live_player_list/live_player_list', function (data) {
      that.setData({
        bottom_nav_data: data,
        nav_bgColor: data[0].bgcolor
      });
    });

    var username = options.username;
    wx.setNavigationBarTitle({
      title: username
    });
    if (util.orgid == '12545') {
      wx.setNavigationBarTitle({
        title: tc == 1 ? '線上直播間' : '线上直播间'
      });
    }
    this.setData({
      logo_url: options.logo_url ? options.logo_url : '',
      goodTypeId: options.cataid ? options.cataid : '',
      postuid: options.uid ? options.uid : '',
    });


    //读取登录用户信息
    app.LoginUserInfo(function (res) {
      if (res && res != -1) {
        that.setData({
          uid: res.uid,
          token: res.token
        });
      }
    }, '');

    that.setData({
      windowWidth: wx.getSystemInfoSync().windowWidth,
      windowHeight: wx.getSystemInfoSync().windowHeight
    });

  },

  //获取直播的banner轮播
  getzhibopic: function () {
    // http://admin.jawanology.com/api.php?ac=getzhibourl&publickey=ZGZvI1mi8Q&binduid=14057
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getzhibopic"
      },
      success: function (responseData) {
        console.log("getzhibopic===", responseData.data);
        var result = responseData.data;
        that.setData({
          swiperImgUrls: result
        });
       
      }
    });
  },

  //获取
  getLiveList: function () {
    // http://admin.jawanology.com/api.php?ac=getzhibourl&publickey=ZGZvI1mi8Q&binduid=14057
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getzhibourl"
      },
      success: function (responseData) {
        console.log("直播数据：", responseData.data);
        that.setData({
          liveListInfo: responseData.data
        });
      }
    });
  },

  goAtherPage: function (e) {
    var abInfo = e.currentTarget.dataset.info 
    var url_base64 = base64.encode(abInfo.play);
    var path = "/pages/zhibo/zhibo?title=" + abInfo.title + "&weburl=" + url_base64;
    wx.navigateTo({ url: path })
  },

  swiperAction: function (e) {
    var that = this;
    var data = e.currentTarget.dataset.info;
    app.saveFormId(e.detail.formId);
    console.log("swiper data:", data);
    if (data) {
      var zhiboInfo = '';
      for (var i = 0; i < this.data.liveListInfo.length; i++) {
        var zbInfo = this.data.liveListInfo[i];
        if (data.zhiboid == zbInfo.id) {
          zhiboInfo = zbInfo;
          break;
        }
      }
      if (zhiboInfo && zhiboInfo.play) {
        var url = zhiboInfo.play;
        var url_base64 = base64.encode(url);
        var path = "/pages/zhibo/zhibo?title=" + zhiboInfo.title + "&weburl=" + url_base64;
        wx.navigateTo({ url: path })
      }

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }, 


  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    });
    this.setData({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },

  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);

    var path = e.detail.value.nav_path;
    console.log("path", e.detail.value)
    console.log("path",path)
    if (path) {
      if (path.indexOf('navto:') == 0) {
        var navPath = path.slice(6);
        wx.navigateTo({
          url: navPath,
        })
      } else {
        wx.redirectTo({ url: path });
      }

    }

    path = e.detail.value.page_path;
    if (path) {
      wx.navigateTo({ url: path });
    }
  }

})